<template>
  <el-dialog
    v-model="visible"
    title="批量忽略资产"
    width="600px"
    :close-on-click-modal="false"
  >
    <div class="batch-ignore-content">
      <div class="info-section">
        <p>
          已选择 <strong>{{ selectedIds.length }}</strong> 个资产进行批量忽略
        </p>
        <p class="warning-text">⚠️ 忽略后的资产将不再显示在待处理列表中</p>
      </div>

      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="忽略原因" prop="ignored_reason">
          <el-input
            v-model="form.ignored_reason"
            type="textarea"
            :rows="4"
            placeholder="请输入忽略原因（可选）"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="warning" :loading="submitting" @click="handleSubmit">
        确认忽略
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { ElMessage, ElLoading } from "element-plus";
import { batchIgnorePanAssets } from "../../../servers";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  selectedIds: { type: Array as () => number[], default: () => [] },
});

const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value),
});

const formRef = ref();
const submitting = ref(false);
const form = ref({
  ignored_reason: "",
});

const rules = {
  // 忽略原因是可选的
};

// 监听弹窗打开，初始化表单
watch(visible, (newVal) => {
  if (newVal) {
    form.value.ignored_reason = "";
  }
});

// 提交
const handleSubmit = async () => {
  if (props.selectedIds.length === 0) {
    ElMessage.warning("请先选择要忽略的资产");
    return;
  }

  submitting.value = true;
  try {
    const params: any = {
      asset_ids: props.selectedIds,
    };

    if (form.value.ignored_reason) {
      params.ignored_reason = form.value.ignored_reason;
    }

    const result = await batchIgnorePanAssets(params);

    const ignoredCount = result.data.ignored_count || 0;
    const totalCount = result.data.total_count || 0;
    ElMessage.success(`成功忽略 ${ignoredCount}/${totalCount} 个资产`);
    emit("success");
    emit("update:modelValue", false);
  } catch (error: any) {
    ElMessage.error(error?.message || "批量忽略失败");
  } finally {
    submitting.value = false;
  }
};

// 取消
const handleCancel = () => {
  emit("update:modelValue", false);
};
</script>

<style scoped lang="scss">
.batch-ignore-content {
  .info-section {
    margin-bottom: 20px;
    padding: 15px;
    background-color: #fff7e6;
    border: 1px solid #ffe58f;
    border-radius: 4px;

    p {
      margin: 5px 0;
      color: #606266;

      strong {
        color: #409eff;
        font-size: 16px;
      }

      &.warning-text {
        color: #fa8c16;
        font-size: 13px;
        margin-top: 10px;
      }
    }
  }
}
</style>
